package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.DeliveryAddress;
import com.dingreading.cloud.admin.entity.table.DeliveryAddressTableDef;
import com.dingreading.cloud.admin.mapper.DeliveryAddressMapper;
import com.dingreading.cloud.admin.service.DeliveryAddressService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class DeliveryAddressServiceImpl extends BaseServiceImpl<DeliveryAddressMapper, DeliveryAddress, DeliveryAddressTableDef> implements DeliveryAddressService {

    @Override
    protected DeliveryAddressTableDef getTable() {
        return DeliveryAddressTableDef.deliveryAddress;
    }

    @Resource
    DeliveryAddressMapper deliveryAddressMapper;

    @Override
    public Page<DeliveryAddress> pageList(PageUtil pageUtil, String storeUid, String staffUid, String name, Integer status) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (storeUid != null)
            condition.and(table.storeUid.eq(storeUid));
        if (staffUid != null)
            condition.and(table.staffUid.eq(staffUid));
        if (StringUtils.isNotBlank(name))
            condition.and(table.name.like(name)
                    .or(table.phone.like(name)));
        if (status != null)
            condition.and(table.status.eq(status));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.status.desc(), table.editTime.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }


    @Override
    public void deleteByStaffIdAndId(String staffUid, Long id) {
        getMapper().deleteByCondition(table.staffUid.eq(staffUid).and(table.id.eq(id)));
    }

    @Override
    public List<DeliveryAddress> getByStoreUidAndStaffUid(String storeUid, String staffUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.status.desc());

        return getMapper().selectListByQuery(wrapper);
    }

}
